﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.Templet;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormQueryCustomer : FormTemplet
    {
        public FormQueryCustomer()
        {
            this.InitializeComponent();
        }

        private void FormQueryCustomer_Load(object sender, EventArgs e)
        {
            this.cbCondition.SelectedIndex = 0;
            if (!LoginInfo.Role[PowerName.Coustomer_Delete])
            {
                this.btnDelete.Enabled = false;
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            string text = "SELECT PKID as 序号,CompanyName as 公司,Leader as 负责人,LeaderPhone as 负责人电话,CompanyPhone as 公司电话,Fax as 传真,CompanyAddress as 公司地址,Linkman as 联系人,LinkmanPhone as 联系人电话,Email as 邮件,Type as 客户类型 FROM Customer";
            if (this.cbCondition.SelectedIndex == 0)
            {
                if (this.rbChinese.Checked)
                {
                    text = text + " WHERE CompanyName LIKE '%" + this.txtCondition.Text.Trim() + "%'";
                }
                else
                {
                    text = text + " WHERE dbo.GetPY(CompanyName) LIKE '%" + this.txtCondition.Text.Trim().Replace("'", "''") + "%'";
                }
            }
            if (this.cbCondition.SelectedIndex == 1)
            {
                text = text + " WHERE LeaderPhone LIKE '%" + this.txtCondition.Text.Trim() + "%'";
            }
            if (this.cbCondition.SelectedIndex == 2)
            {
                if (this.rbChinese.Checked)
                {
                    text = text + " WHERE CompanyAddress LIKE '%" + this.txtCondition.Text.Trim() + "%'";
                }
                else
                {
                    text = text + " WHERE dbo.GetPY(CompanyAddress) LIKE '%" + this.txtCondition.Text.Trim().Replace("'", "''") + "%'";
                }
            }
            if (this.cbCondition.SelectedIndex == 3)
            {
                if (this.rbChinese.Checked)
                {
                    text = text + " WHERE Linkman LIKE '%" + this.txtCondition.Text.Trim() + "%'";
                }
                else
                {
                    text = text + " WHERE dbo.GetPY(Linkman) LIKE '%" + this.txtCondition.Text.Trim().Replace("'", "''") + "%'";
                }
            }
            if (this.cbCondition.SelectedIndex == 4)
            {
                text = text + " WHERE LinkmanPhone LIKE '%" + this.txtCondition.Text.Trim() + "%'";
            }
            if (this.cbCondition.SelectedIndex == 5)
            {
                text = text + " WHERE Email LIKE '%" + this.txtCondition.Text.Trim() + "%'";
            }
            Customers customers = new Customers();
            DataTable dataTable = null;
            try
            {
                dataTable = customers.GetCustomers(text);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message + "\r\n获取数据时出现错误，请从试", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            customers.Dispose();
            if (dataTable != null)
            {
                this.dgCustomer.DataSource = null;
                this.dgCustomer.DataSource = dataTable;
                this.dgCustomer.AutoResizeColumns();
                dataTable.Dispose();
            }
        }

        private void btnUpdateCustomer_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Coustomer_Update])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                try
                {
                    string text = this.dgCustomer[0, 0].Value.ToString();
                }
                catch (Exception)
                {
                    MessageBox.Show("表格中没有数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                int rowIndex = this.dgCustomer.CurrentCell.RowIndex;
                new FormUpdateCustomer(Convert.ToInt32(this.dgCustomer.Rows[rowIndex].Cells[0].Value), this.dgCustomer.Rows[rowIndex].Cells[1].Value.ToString(), this.dgCustomer.Rows[rowIndex].Cells[2].Value.ToString(), this.dgCustomer.Rows[rowIndex].Cells[3].Value.ToString(), this.dgCustomer.Rows[rowIndex].Cells[4].Value.ToString(), this.dgCustomer.Rows[rowIndex].Cells[5].Value.ToString(), this.dgCustomer.Rows[rowIndex].Cells[6].Value.ToString(), this.dgCustomer.Rows[rowIndex].Cells[7].Value.ToString(), this.dgCustomer.Rows[rowIndex].Cells[8].Value.ToString(), this.dgCustomer.Rows[rowIndex].Cells[9].Value.ToString(), this.dgCustomer.Rows[rowIndex].Cells[10].Value.ToString())
                {
                    MdiParent = base.MdiParent
                }.Show();
            }
        }

        private void btnShowOrdersForCustomer_Click(object sender, EventArgs e)
        {
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void cbCondition_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cbCondition.SelectedIndex == 1 || this.cbCondition.SelectedIndex == 4 || this.cbCondition.SelectedIndex == 5)
            {
                this.panelOption.Enabled = false;
            }
            else
            {
                this.panelOption.Enabled = true;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Coustomer_Delete])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this.dgCustomer.Rows.Count < 1)
            {
                MessageBox.Show("没有数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (MessageBox.Show("你确定要删除吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.Cancel)
            {
                Customers customers = new Customers();
                bool flag = false;
                try
                {
                    flag = customers.Delete(Convert.ToInt32(this.dgCustomer.Rows[this.dgCustomer.CurrentRow.Index].Cells[0].Value));
                }
                catch (SqlException ex)
                {
                    int number = ex.Number;
                    if (number != 547)
                    {
                        MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    else
                    {
                        MessageBox.Show("操作失败，在客户下面没有包含计划和订单信息时才能删除客户！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                customers.Dispose();
                if (flag)
                {
                    MessageBox.Show("操作成功！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    this.dgCustomer.Rows.Remove(this.dgCustomer.CurrentRow);
                }
            }
        }
    }
}
